#include<stdio.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/ioctl.h>
#include<errno.h>
#include<string.h>
#include<stdlib.h>
#include<strings.h>

int get_file_size1(int fd){
    struct stat st;
    bzero(&st, sizeof(st));
    fstat(fd, &st);
    return st.st_size;
}

int get_file_size2(int fd){
    char ch;
    int file_size = 0;
    while(1){
        if(read(fd, &ch, 1) > 0)
            file_size++;
        else break;
    }
    return file_size;
}

int get_file_size3(FILE *fp){
    char buf;
    int file_size = 0;
    while(1){
        if(fread(&buf, sizeof(char), 1, fp))
            file_size++;
        else break;
    }
    return file_size;
}

int get_file_size4(int fd){
    return lseek(fd, 0, SEEK_END);
}

int main(int argc,char *argv[]){
    int fd = open("test.txt", O_RDWR|O_CREAT);
    FILE *fp = fopen("test.txt", "r");
    printf("文件大小：%d\n", get_file_size1(fd));
    printf("文件大小：%d\n", get_file_size2(fd));
    printf("文件大小：%d\n", get_file_size3(fp));
    printf("文件大小：%d\n", get_file_size4(fd));
    close(fd);
    fclose(fp);
}